bcimport base64
import os.path
import time
from datetime import timedelta
from urllib.parse import urlencode

import jinja2
from selenium import webdriver

# 导入应用组件
import bc_constants
import bc_functions


def generate_bc(datetime_input=""):
    print("本地日期为 : ", datetime_input)
    # 时间
    datetimeReal = bc_functions.get_real_datetime(datetime_input)
    dateAppointStr = datetimeReal.strftime(bc_constants.FORMAT_DATE)

    print("本地日期为 : ", dateAppointStr)

    # 路径
    pathScreenshot = bc_constants.PATH_SCREENSHOT + dateAppointStr + "/"

    startDatetime = (datetimeReal + timedelta(days=-1)).strftime(bc_constants.FORMAT_DATETIME)

    endDatetime = datetimeReal.strftime(bc_constants.FORMAT_DATETIME)

    print("数据开始时间为 : ", startDatetime)
    print("数据结束时间为 : ", endDatetime)

    uriBaseParams = {
        bc_constants.URI_ARG_ENV: bc_constants.ENV_PRD,
        bc_constants.URI_ARG_START: startDatetime,
        bc_constants.URI_ARG_END: endDatetime,
        bc_constants.URI_ARG_APPID: ""
    }

    ot = {
        "date_appoint": dateAppointStr,
        "author": bc_constants.AUTHOR
    }

    if not os.path.exists(pathScreenshot):
        os.mkdir(pathScreenshot)

    cookie1 = {
        "domain": bc_constants.URI_DOMAIN,
        "name": bc_constants.URI_COOKIE_NAME_ID,
        "value": bc_constants.URI_COOKIE_VALUE_ID
    }
    cookie2 = {
        "domain": bc_constants.URI_DOMAIN,
        "name": bc_constants.URI_COOKIE_NAME_VAN,
        "value": bc_constants.URI_COOKIE_VALUE_VAN
    }

    browser = webdriver.Chrome(executable_path=bc_constants.FILE_BIN_CHROME_DRIVER)

    # 数据便利
    for appId, dataAlias in bc_constants.SERVICE_MAP_DATA.items():
        dateTypes = bc_constants.URI_MAP_ALIAS_DATA[dataAlias]
        for name, dateType in dateTypes.items():
            uriBizParams = uriBaseParams
            uriBizParams[bc_constants.URI_ARG_APPID] = appId
            uriBiz = bc_constants.URI_BASE + dateType + "?" + urlencode(uriBizParams)
            print("数据请求链接 : " + uriBiz)

            fileNameScreenshot = pathScreenshot + appId + "." + name + ".png"
            browser.get(uriBiz)
            browser.add_cookie(cookie_dict=cookie1)
            browser.add_cookie(cookie_dict=cookie2)
            time.sleep(bc_constants.SLEEP_NOM)
            browser.get_screenshot_as_file(fileNameScreenshot)
            fileSourceScreenshot = open(fileNameScreenshot, "rb")
            ot[appId + '.' + name] = bc_constants.PREFIX_BASE64_PNG + base64.b64encode(
                fileSourceScreenshot.read()).decode(
                'ascii')
            os.remove(fileNameScreenshot)

    print("模版数据为 : ", ot)

    TemplateLoader = jinja2.FileSystemLoader(os.path.abspath(bc_constants.PATH_TPL))
    TemplateEnv = jinja2.Environment(loader=TemplateLoader)
    template = TemplateEnv.get_template(bc_constants.TPL_NAME_bc)
    content = template.render({"ot": ot})

    filePathReport = bc_constants.PATH_REPORT + "regular_report_" + dateAppointStr + '.html'

    current_path = os.getcwd()

    fileSourceReport = open(filePathReport, 'w', encoding='utf-8')

    fileSourceReport.write(content)
    fileSourceReport.close()
    browser.close()
    return filePathReport, "bc/report/bc_" + dateAppointStr + '.html'

